package com.sibu.yx.promotion.service.impl.excel;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.doubo.common.util.ExceptionUtil;
import com.sibu.yx.promotion.persistence.request.ImportDemoRequest;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @author suzheng
 * @date 2020-06-17
 **/
@Slf4j
public class ImportExcelEventDemoListener extends AnalysisEventListener<ImportDemoRequest> {

    List<ImportDemoRequest> failResultList = new ArrayList<>();

    public List<ImportDemoRequest> getFailResultList() {
        return failResultList;
    }


    @Override
    public void invoke(ImportDemoRequest expressInfoRequest, AnalysisContext context) {
        StringBuilder errMsg = new StringBuilder();

        if (errMsg.length()==0) {
            try {
                // do something
            } catch (Exception e) {
                errMsg.append(ExceptionUtil.getAsString(e));
                log.error("导入异常数据：【{}】【{}】", expressInfoRequest,errMsg);
            }
        }
        if(StrUtil.isNotBlank(errMsg)){
            expressInfoRequest.setErrMsg(errMsg.toString());
            failResultList.add(expressInfoRequest);
        }

    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        log.info("所有数据解析完毕");
    }
}
